home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 04 Mommersteeg / Tennis / Paddle.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-23  |  6.3 KB  |  180 lines

  1. //----------------------------------------------------------------------------------------------
  2. // Sequential Prediction Demo: The positioning pattern
  3. // 
  4. // Author:  Fri Mommersteeg
  5. // Date:    10-09-2001
  6. // File:    Paddle.cpp
  7. //----------------------------------------------------------------------------------------------
  8.  
  9. //----------------------------------------------------------------------------------------------
  10. // Include files
  11. //----------------------------------------------------------------------------------------------
  12.  
  13. #include "stdafx.h"
  14. #include "paddle.h"
  15. #include <math.h>
  16.  
  17. //----------------------------------------------------------------------------------------------
  18. // Initialize static member
  19. //----------------------------------------------------------------------------------------------
  20.  
  21. int CPaddle::instantiations = 0;
  22.  
  23. //----------------------------------------------------------------------------------------------
  24. // ResetForService(): Resets the paddle for the service
  25. //----------------------------------------------------------------------------------------------
  26.  
  27. void CPaddle::ResetForService(BOOL HasService) {
  28.     if (direction == PADDLE_DOWN) {
  29.         SetPosition((field.right+field.left) / 2, (2*field.top+field.bottom) / 3);
  30.     } else {
  31.         SetPosition((field.right+field.left) / 2, (field.top+2*field.bottom) / 3);
  32.     }
  33. }
  34.  
  35. //----------------------------------------------------------------------------------------------
  36. // Left(): Move paddle to the left
  37. //----------------------------------------------------------------------------------------------
  38.  
  39. void CPaddle::Left() {
  40.     if (px>field.left) {
  41.         px -= NormSpeed(STEP_SIZE);
  42.     }
  43.     RecalculatePaddle();
  44. }
  45.  
  46. //----------------------------------------------------------------------------------------------
  47. // Right(): Move paddle to the right
  48. //----------------------------------------------------------------------------------------------
  49.     
  50. void CPaddle::Right() {
  51.     if (px<field.right) {
  52.         px += NormSpeed(STEP_SIZE);
  53.     }
  54.     RecalculatePaddle();
  55. }
  56.  
  57. //----------------------------------------------------------------------------------------------
  58. // Up(): Move paddle up
  59. //----------------------------------------------------------------------------------------------
  60.  
  61. void CPaddle::Up() {
  62.     if (py>field.top) {
  63.         py -= NormSpeed(STEP_SIZE);
  64.     }
  65.     RecalculatePaddle();
  66. }
  67.  
  68. //----------------------------------------------------------------------------------------------
  69. // Down(): Move paddle down
  70. //----------------------------------------------------------------------------------------------
  71.  
  72. void CPaddle::Down() {
  73.     if (py<field.bottom) {
  74.         py += NormSpeed(STEP_SIZE);
  75.     }
  76.     RecalculatePaddle();
  77. }
  78.  
  79. //----------------------------------------------------------------------------------------------
  80. // Rotate(): Rotate the paddle
  81. //----------------------------------------------------------------------------------------------
  82.  
  83. void CPaddle::Rotate(int angle) {
  84.     if (abs(rotation+angle)<MAX_ROTATION) {
  85.         rotation += (int)NormSpeed(angle);
  86.         nx = (float)cos((float)(rotation+90)/57.3);
  87.         ny = (float)sin((float)(rotation+90)/57.3);
  88.     }
  89.     RecalculatePaddle();
  90. }
  91.  
  92. //----------------------------------------------------------------------------------------------
  93. // RecalculatePaddle(): Recalculate the shape of the paddle
  94. //----------------------------------------------------------------------------------------------
  95.  
  96. void CPaddle::RecalculatePaddle() {
  97.     paddle[0].x = (int)(px - (PADDLE_SIZE*cos((float)rotation/57.3)));
  98.     paddle[0].y = (int)(py - (PADDLE_SIZE*sin((float)rotation/57.3)));
  99.     paddle[1].x = (int)(px + (PADDLE_SIZE*cos((float)rotation/57.3)));
  100.     paddle[1].y = (int)(py + (PADDLE_SIZE*sin((float)rotation/57.3)));
  101.     paddle[2].x = paddle[1].x+(int)(PADDLE_WIDTH*nx);
  102.     paddle[2].y = paddle[1].y+(int)(PADDLE_WIDTH*ny);
  103.     paddle[3].x = paddle[0].x+(int)(PADDLE_WIDTH*nx);
  104.     paddle[3].y = paddle[0].y+(int)(PADDLE_WIDTH*ny);
  105.  
  106.     if (region != NULL) {
  107.         DeleteObject(region);
  108.     }
  109.     region = CreatePolygonRgn(paddle, 4, ALTERNATE);
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------------
  113. // BallCollision(): Check for a collision between this paddle and the ball
  114. //----------------------------------------------------------------------------------------------
  115.  
  116. void CPaddle::BallCollision(CTennisBall * ball) {
  117.     RECT ballRect;
  118.     ball->GetBoundingRect(&ballRect);
  119.     if (ball->GetHeight()<PADDLE_HEIGHT) {
  120.         if (RectInPaddle(&ballRect)) {
  121.             ball->SetOwner(id);
  122.             NotifyAll(HIT_BALL_EVENT, (DWORD)this);
  123.             ball->BounceBall((float)direction*(HIT_FORCE*nx), (float)direction*(HIT_FORCE*ny));
  124.             BOOL StillInPaddle = TRUE;
  125.             while (StillInPaddle) {
  126.                 ball->Update();
  127.                 ball->GetBoundingRect(&ballRect);
  128.                 StillInPaddle = RectInPaddle(&ballRect);
  129.             }
  130.             // at the end of the collision the ball and paddle no longer collide
  131.             OnBallCollision();
  132.         }
  133.     }
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------------
  137. // PtInPaddle(): Checks if a point is inside the shape of the paddle
  138. //----------------------------------------------------------------------------------------------
  139.  
  140. BOOL CPaddle::PtInPaddle(int x, int y) {
  141.     return PtInRegion(region, x, y);
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------------
  145. // RectInPaddle(): Checks if a rectangle is overlaps the shape of the paddle
  146. //----------------------------------------------------------------------------------------------
  147.  
  148. BOOL CPaddle::RectInPaddle(const RECT * lpRect) {
  149.     return RectInRegion(region, lpRect);
  150. }
  151.  
  152. //----------------------------------------------------------------------------------------------
  153. // Paint(): Paints the paddle
  154. //----------------------------------------------------------------------------------------------
  155.  
  156. void CPaddle::Paint(LPDDS lpdds) {
  157.         
  158.     HDC dc;
  159.     lpdds->GetDC(&dc);    
  160.  
  161.     HBRUSH brush = CreateSolidBrush(color);
  162.     HBRUSH oldBrush = (HBRUSH)SelectObject(dc, brush);
  163.     HPEN pen = CreatePen(PS_NULL, 0, 0);
  164.     HPEN oldPen = (HPEN)SelectObject(dc, pen);
  165.     
  166.     SetPolyFillMode(dc, ALTERNATE);
  167.     Polygon(dc, paddle, 4);
  168.     
  169.     if (oldPen != NULL) {
  170.         SelectObject(dc, oldPen);
  171.         DeleteObject(brush);
  172.     }
  173.     if (oldBrush != NULL) {
  174.         SelectObject(dc, oldBrush);
  175.         DeleteObject(pen);
  176.     }
  177.  
  178.     lpdds->ReleaseDC(dc);
  179. }
  180.